﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using QLBH.DataModel;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    public partial class AddMatHangDlg : Form
    {
        public eDlgType DlgType { set; get; }
        public IQLMatHangModel Model { set; get; }
        //bien nay chi duoc dung trong khi lap phieu mua hang
        public string TenMatHang{get;set;}
        public AddMatHangDlg()
        {
            InitializeComponent();

           
           
            if (DlgType==eDlgType.ADD)
            {
                Model = ModelFactory.ModelFactory.GetQLMatHangModelInstance();
                Model.MHCODE = CodeHelper.GetNewCode();
                Model.MHID = Guid.NewGuid();
                Model.NGAYDCGIABANBUON = DateTime.Now;
                Model.NGAYDCGIABANLE = DateTime.Now;
                Model.NGAYDCGIANHAP = DateTime.Now;

            }
           

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckContrain())
            {
                try
                {
                    UpdateModelFromControl();
                    if (DlgType == eDlgType.ADD)
                    {
                        MATHANG entity = MatHangController.GetInstance().Add(Model.ToEntity());
                        NCC_MATHANG ncc_mh_entity = new NCC_MATHANG();
                        ncc_mh_entity.CHIETKHAU = Model.CHIETKHAU;
                        ncc_mh_entity.GIANHAP = Model.GIANHAP;
                        ncc_mh_entity.NCCID = Model.NCCID;
                        ncc_mh_entity.MHID = entity.MHID;

                        NCC_MatHangController.GetInstance().Add(ncc_mh_entity);
                    }
                    else
                    {
                        MatHangController.GetInstance().Edit(Model.ToEntity());
                    }
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        bool CheckContrain()
        {
            try
            {
                try
                {
                    DataInputHelper.CheckTextData(tbxMaHang.Text, "Mã mặt hàng", 14);
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxMaHang.SelectAll();
                    tbxMaHang.Focus();
                    return false;
                }
                try
                {
                    DataInputHelper.CheckTextData(tbxTenHang.Text, "Tên mặt hàng", 255);
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxTenHang.SelectAll();
                    tbxTenHang.Focus();
                    return false;
                }
                try
                {
                    DataInputHelper.CheckDecimalData(tbxGiaNhap.Text, "giá nhập");
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxGiaNhap.SelectAll();
                    tbxGiaNhap.Focus();
                    return false;
                }
                try
                {
                    DataInputHelper.CheckDecimalData(tbxGiaBanLe.Text, "giá bán lẻ");
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxGiaBanLe.SelectAll();
                    tbxGiaBanLe.Focus();
                    return false;
                }
                try
                {
                    DataInputHelper.CheckDecimalData(tbxGiaBanBuon.Text, "giá Bán buôn");
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxGiaBanBuon.SelectAll();
                    tbxGiaBanBuon.Focus();
                    return false;
                }

                try
                {
                    DataInputHelper.CheckDecimalData(tbxChietKhau.Text, "Chiết khấu");
                }
                catch (System.Exception ex)
                {
                    ShowMessage.Instance.ShowError(ex.Message);
                    tbxChietKhau.SelectAll();
                    tbxChietKhau.Focus();
                    return false;
                }
                
                
                if (cmbDonVi.SelectedItem==null)
                {
                    cmbDonVi.Focus();
                    return false;
                }
                if (cmbNCC.SelectedItem==null)
                {
                    cmbNCC.Focus();
                    return false;
                }
                if (cmbNhomhang.SelectedItem==null)
                {
                    cmbNhomhang.Focus();
                    return false;
                }
                return true;
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                return false;
            }
        }
        private void AddMatHangDlg_Load(object sender, EventArgs e)
        {
            if (TenMatHang!=null)
            {
                tbxTenHang.Text = TenMatHang;
                tbxTenHang.Enabled = false;
            }
            LoadDSNhomHang();
            LoadDsDonviTinh();
            LoadAllNhaCC();
            if (DlgType==eDlgType.ADD)
            {
                this.Text = "Thêm mới mặt hàng";
                tbxMaHang.Text = Model.MHCODE;
               
            }else
            {
                this.Text = "Thay đổi thông tin mặt hàng";
                LoadModelToForm();
            }
        }
        private void UpdateModelFromControl()
            
        {
            Model.DVTID = (Guid)cmbDonVi.SelectedValue;
            Model.GHICHU = rtbGhiChu.Text;
            Model.GIABANBUON = decimal.Parse(tbxGiaBanBuon.Text);
            Model.GIABANLE = decimal.Parse(tbxGiaBanLe.Text);
            Model.GIANHAP = decimal.Parse(tbxGiaNhap.Text);
            Model.MHTEN = tbxTenHang.Text;
            Model.NCCID = (Guid)cmbNCC.SelectedValue;
            Model.NHOMHANGID = (Guid)cmbNhomhang.SelectedValue;
            Model.DVTID = (Guid)cmbDonVi.SelectedValue;
            Model.CHIETKHAU = decimal.Parse(tbxChietKhau.Text);
           
            Model.XUATXU = tbxXuatXu.Text;
        }
        private void lbAddDVT_Click(object sender, EventArgs e)
        {
            AddDVTDlg addDVT = new AddDVTDlg();
            if (addDVT.ShowDialog()==DialogResult.OK)
            {
                LoadDsDonviTinh();
                cmbDonVi.SelectedValue = addDVT.Model.DVTID;
            }
        }

        private void lbAddNhomHang_Click(object sender, EventArgs e)
        {
            AddNhomHangDlg addNhomhang = new AddNhomHangDlg();
            if (addNhomhang.ShowDialog() == DialogResult.OK)
            {
                LoadDSNhomHang();
                cmbNhomhang.SelectedItem = addNhomhang.Model;
            }
        }
        void LoadModelToForm()
        {
            tbxMaHang.Text = Model.MHCODE;
            tbxTenHang.Text = Model.MHTEN;
            tbxXuatXu.Text = Model.XUATXU;
            tbxGiaBanBuon.Text = Model.GIABANBUON.ToString();
            tbxGiaBanLe.Text = Model.GIABANLE.ToString();
            tbxGiaNhap.Text = Model.GIANHAP.ToString();
            cmbNhomhang.SelectedValue = Model.NHOMHANGID;
            cmbNCC.SelectedValue = Model.NCCID;
            cmbDonVi.SelectedValue = Model.DVTID;
            tbxChietKhau.Text = Model.CHIETKHAU.ToString();
        }
        private void LoadDSNhomHang()
        {
            var items = NhomHangController.GetInstance().GetAll();
            cmbNhomhang.DataSource = items;
            cmbNhomhang.DisplayMember = "TENLOAIHANG";
            cmbNhomhang.ValueMember = "LOAIHANGID";
        }

        private void lbAddNCC_Click(object sender, EventArgs e)
        {
            AddNhaCungCapDlg addNCC = new AddNhaCungCapDlg();
            if (addNCC.ShowDialog()==DialogResult.OK)
            {
                LoadAllNhaCC();
                cmbNCC.SelectedValue = addNCC.Model.NCCUID;
            }
        }
        private void LoadDsDonviTinh()
        {
            var item = DVTController.GetInstance().GetAll();
            cmbDonVi.DataSource = item;
            
            cmbDonVi.DisplayMember = "TENDVT";
            cmbDonVi.ValueMember = "DVTID";
        }
        private void LoadAllNhaCC()
        {

            var items = NCCController.GetInstance().GetAll();
            cmbNCC.DataSource = items;
            cmbNCC.DisplayMember = "TENNCC";
            cmbNCC.ValueMember = "NCCUID";
        }

        private void MatHangDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void tbxThue_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbxSoluong_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSoluong_TextChanged(object sender, EventArgs e)
        {
            string text = "";
        }
    }
}
